package chapter07

import java.text.SimpleDateFormat

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}


/**
 * author: 余辉
 * blog: https://blog.csdn.net/silentwolfyh
 * descriptions:
 * date: 2024 - 09 - 02 11:32 上午
 */
object DateUtilObj {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setAppName("DateUtilObj")
      .setMaster("local[*]") //本地模式，开多个线程

    //1.创建SparkContext
    val sc = new SparkContext(conf)

    val lines = sc.textFile("doc/input/06.txt")

    lines.foreach(println)

    val timeRDD: RDD[Long] = lines.map(e => {
      //将字符串转成long类型时间戳
      //使用自定义的object工具类
      val time: Long = DateUtilObj.parse(e)
      time
    })

    val res = timeRDD.collect()
    println(res.toBuffer)
  }

  //多个Task使用了一个共享的SimpleDateFormat，SimpleDateFormat是线程不安全

  val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

  //线程安全的
  //val sdf: FastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss")

  def parse(str: String): Long = {
    //2022-05-23 11:39:30
    sdf.parse(str).getTime
  }

}